<template>
  <el-dialog
    title="编辑交易密码"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :before-close="handleCloseDialog"
    width="680px"
  >
    <el-form
      :model="dataObject"
      :rules="rules"
      ref="adminForm"
      label-width="100px"
      size="small"
    >
      <el-form-item label="交易密码：" prop="cashPwd">
        <el-input
          v-model="dataObject.cashPwd"
          type="password"
          placeholder="请输入交易密码"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="handleConfirmDialog('adminForm')"
        size="mini"
        :loading="loading"
        >确 定</el-button
      >
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { updataCashPwd } from "@/api/member/index";
import { createNamespacedHelpers } from "vuex";
const { mapState } = createNamespacedHelpers("member");
const defaultObject = {
  cashPwd: ""
};

export default {
  data() {
    return {
      visible: true,
      loading: false,
      dataObject: Object.assign({}, defaultObject)
    };
  },
  props: {
    row: {
      type: Object,
      default: {}
    }
  },
  computed: {
    ...mapState({
      rules: state => state.rules
    })
  },
  mounted() {
    Object.keys(this.dataObject).forEach(key => {
      this.dataObject[key] = this.row[key];
    });
  },
  methods: {
    handleCloseDialog() {
      this.$emit("handle-cancel");
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true;

          updataCashPwd({
            password: this.dataObject.cashPwd,
            id: this.row.id
          })
            .then(response => {
              this.resetForm(formName);
              this.$message({
                message: "更改成功",
                type: "success",
                duration: 1000
              });
              this.loading = false;
              this.$emit("handle-confirm");
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    }
  }
};
</script>
<style scoped>
.user-container {
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;
}

.left-box {
  border: 1px solid #dcdfe6;
  padding: 4px;
}

.right-box {
  border: 1px solid #dcdfe6;
  padding: 4px;
  margin-left: 8px;
  flex-grow: 1;
}

.ritht-box-top {
  border: 1px solid #dcdfe6;
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;
  justify-items: center;
  padding: 0px 6px;
  border-radius: 4px;
}

.ritht-box-top span {
  font-weight: 100;
  line-height: 26px;
}

.ritht-box-content {
  height: 400px;
  overflow: auto;
  border: 1px solid #dcdfe6;
  margin-top: 4px;
}

.tree-list {
  height: 400px;
  overflow: auto;
  border: 1px solid #dcdfe6;
  margin-top: 4px;
}

.tag-item {
  margin: 4px 0px 0px 4px;
}
</style>
